package com.ea.report.schedule;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ea.report.mapper.*;
import com.ea.report.pojo.po.StatsGroupEmotionalMap;
import com.ea.report.pojo.po.StatsGroupPersonalityMap;
import com.ea.report.pojo.po.StatsWarningProperties;
import com.ea.report.pojo.po.StatsWarningType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
//@EnableScheduling
@Slf4j
public class ReportStatusSchedule {
    @Resource
    ReportInfoMapper reportInfoMapper;
    @Resource
    StatsWarningTypeMapper statsWarningTypeMapper;
    @Resource
    StatsWarningPropertiesMapper statsWarningPropertiesMapper;
    @Resource
    StatsGroupEmotionalMapMapper statsGroupEmotionalMapMapper;
    @Resource
    StatsGroupPersonalityMapMapper statsGroupPersonalityMapMapper;

    // 秒 分 时 日 月 星期
    @Scheduled(cron = "0 0 2 * * ?")
    public void updateReportProperties() {
        updateReportPropertiesByDate(null);
    }

    public void updateReportPropertiesByDate(String date) {
        // 更新统计表
        Page<StatsWarningProperties> page = new Page<>(1, 100);
        String nowDate;
        String startDate;
        String endDate;
        if (date == null) {
            nowDate = LocalDate.now().minusDays(1).toString();
            startDate = nowDate + " 00:00:00";
            endDate = nowDate + " 23:59:59";
        } else {
            startDate = date + " 00:00:00";
            endDate = date + " 23:59:59";
        }
        Page<StatsWarningProperties> resultPage = reportInfoMapper.selectStatsWarningProperties(startDate, endDate, page);
        long total = resultPage.getTotal();
        for (int i = 1; i <= total / 100 + 1; i++) {
            page.setCurrent(i);
            // 查询预警人数
            List<StatsWarningProperties> statsWarningPropertiesList =
                    reportInfoMapper.selectStatsWarningProperties(startDate, endDate, page).getRecords();

            // 批量更新
            if (!statsWarningPropertiesList.isEmpty()) {
                statsWarningPropertiesMapper.insertBatchSomeColumn(statsWarningPropertiesList);
            }
        }
    }

    @Scheduled(cron = "0 30 2 * * ?")
    public void updateReportType() {
        updateReportTypeByDate(null);
    }

    public void updateReportTypeByDate(String date) {
        // 分页
        Page<StatsWarningType> page = new Page<>(1, 100);
        String nowDate;
        String startDate;
        String endDate;
        if (date == null) {
            nowDate = LocalDate.now().minusDays(1).toString();
            startDate = nowDate + " 00:00:00";
            endDate = nowDate + " 23:59:59";
        } else {
            startDate = date + " 00:00:00";
            endDate = date + " 23:59:59";
        }
        long total = reportInfoMapper.selectStatsWarningType(startDate, endDate, page).getTotal();
        for (int i = 1; i <= total / 100 + 1; i++) {
            page.setCurrent(i);
            // 查询预警人数
            List<StatsWarningType> statsWarningTypeList =
                    reportInfoMapper.selectStatsWarningType(startDate, endDate, page).getRecords();

            // 批量更新
            if (!statsWarningTypeList.isEmpty()) {
                statsWarningTypeMapper.insertBatchSomeColumn(statsWarningTypeList);
            }
        }
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void updateGroupEmotionalMap() {
        updateGroupEmotionalMapByDate(null);
    }

    public void updateGroupEmotionalMapByDate(String date) {
        // 分页
        Page<StatsGroupEmotionalMap> page = new Page<>(1, 100);
        // 计算昨天的日期
        String nowDate;
        String startDate;
        String endDate;
        if (date == null) {
            nowDate = LocalDate.now().minusDays(1).toString();
            startDate = nowDate + " 00:00:00";
            endDate = nowDate + " 23:59:59";
        } else {
            startDate = date + " 00:00:00";
            endDate = date + " 23:59:59";
        }
        long total = reportInfoMapper.selectStatsGroupEmotionalMap(startDate, endDate, page).getTotal();
        for (int i = 1; i <= total / 100 + 1; i++) {
            page.setCurrent(i);
            // 查询预警人数
            List<StatsGroupEmotionalMap> statsGroupEmotionalMapList =
                    reportInfoMapper.selectStatsGroupEmotionalMap(startDate, endDate, page).getRecords();

            // 批量更新
            if (!statsGroupEmotionalMapList.isEmpty()) {
                statsGroupEmotionalMapMapper.insertBatchSomeColumn(statsGroupEmotionalMapList);
            }
        }
    }

    @Scheduled(cron = "0 30 3 * * ?")
    public void updateGroupPersonalityMap() {
        updateGroupPersonalityMapByDate(null);
    }

    public void updateGroupPersonalityMapByDate(String date) {
        // 分页
        Page<StatsGroupPersonalityMap> page = new Page<>(1, 100);
        String nowDate;
        String startDate;
        String endDate;
        if (date == null) {
            nowDate = LocalDate.now().minusDays(1).toString();
            startDate = nowDate + " 00:00:00";
            endDate = nowDate + " 23:59:59";
        } else {
            startDate = date + " 00:00:00";
            endDate = date + " 23:59:59";
        }
        long total = reportInfoMapper.selectStatsGroupPersonalityMap(startDate, endDate, page).getTotal();
        for (int i = 1; i <= total / 100 + 1; i++) {
            page.setCurrent(i);
            // 查询预警人数
            List<StatsGroupPersonalityMap> statsGroupPersonalityMapList =
                    reportInfoMapper.selectStatsGroupPersonalityMap(startDate, endDate, page).getRecords();

            // 批量更新
            if (!statsGroupPersonalityMapList.isEmpty()) {
                statsGroupPersonalityMapMapper.insertBatchSomeColumn(statsGroupPersonalityMapList);
            }
        }
    }

}
